const Base = require('./base.js');
const fs = require('fs');
const _ = require('lodash');

module.exports = class extends Base {
  async infoAction() {
    const userInfo = await this.model('user')
      .where({ id: this.getLoginUserId() })
      .find();
    delete userInfo.password;
    userInfo.coupon = await this.model('user_coupon')
      .alias('uc')
      .field([
        'uc.id as uid',
        'uc.user_id as user_id',
        'uc.used_time,uc.order_id',
        'c.*'
      ])
      .join({
        table: 'coupon',
        join: 'inner',
        as: 'c',
        on: ['uc.coupon_id', 'c.id']
      })
      .where({ user_id: this.getLoginUserId() })
      .select();
    userInfo.myattention = await this.model('attention')
      .field(['vd.*'])
      .alias('at')
      .join({
        table: 'video',
        join: 'inner',
        as: 'vd',
        on: ['at.value_id', 'vd.user_id']
      })
      .where({
        'at.user_id': this.getLoginUserId(),
        'vd.isonline': 1,
        'vd.is_review': 1
      })
      .count('vd.id');
    userInfo.myvideo = await this.model('video')
      .where({ user_id: this.getLoginUserId() })
      .count('id');
    userInfo.attention = await this.model('attention')
      .where({ user_id: this.getLoginUserId() })
      .select();
    return this.json(userInfo);
  }
  async homeAction() {
    const myvideo = await this.model('video')
      .alias('vd')
      .field([
        'vd.*',
        'g.name',
        'g.primary_pic_url',
        'g.retail_price',
        'g.promotion_price',
        'g.list_pic_url',
        'g.id as goods_id'
      ])
      .join({
        table: 'goods',
        join: 'inner',
        as: 'g',
        on: ['vd.goods_id', 'g.id']
      })
      .where({
        'vd.user_id': this.get('user_id'),
        'vd.isonline': 1,
        'vd.is_review': 1
      })
      .page(this.get('page'))
      .countSelect();
    const myattention = await this.model('video')
      .alias('vd')
      .field([
        'vd.*',
        'g.name',
        'g.primary_pic_url',
        'g.retail_price',
        'g.promotion_price',
        'g.list_pic_url',
        'g.id as goods_id',
        'u.is_attention'
      ])
      .join({
        table: 'goods',
        join: 'inner',
        as: 'g',
        on: ['vd.goods_id', 'g.id']
      })
      .join({
        table: 'attention',
        join: 'left',
        as: 'u',
        on: ['vd.user_id', 'u.value_id']
      })
      .where({
        'u.user_id': this.get('user_id'),
        'vd.isonline': 1,
        'vd.is_review': 1
      })
      .page(this.get('page'))
      .countSelect();
    return this.success({ myvideo, myattention });
  }
  /**
   * 保存用户头像
   * @returns {Promise.<void>}
   */
  async saveAvatarAction() {
    const avatar = this.file('avatar');
    if (think.isEmpty(avatar)) {
      return this.fail('保存失败');
    }

    const avatarPath =
      think.RESOURCE_PATH +
      `/static/user/avatar/${this.getLoginUserId()}.` +
      _.last(_.split(avatar.path, '.'));

    fs.rename(avatar.path, avatarPath, function(res) {
      return this.success();
    });
  }
};
